import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-22
 * Time: 19:14
 */
public class test1 {
    /*
    LeetCode 20 有效括号
     */
    public boolean isValid(String s) {
        Stack<Character> s1 = new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch = s.charAt(i);
            // 左括号
            if(ch=='(' || ch=='[' || ch=='{'){
                s1.push(ch);
            }
            // 右括号
            if(ch==')' || ch==']' || ch=='}'){
                if(s1.isEmpty()){
                    return false;
                }
                char ch2 = s1.pop();
                if( (ch!=')' && ch2=='(') || (ch!=']' && ch2=='[') || (ch!='}' &&  ch2=='{') ){
                    return false;
                }
            }
        }
        return s1.isEmpty();
    }
}
